﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

public static class MenuUpdater {
    public static void update(List<Food> foodlist)
    {
        foodlist.Sort();
        try
        {
            StreamWriter output = File.CreateText("Menu.txt");
            for (int i = 0; i < foodlist.Count; ++i)
            {
                output.WriteLine("{0}%{1}", foodlist[i].Name, foodlist[i].Price);
            }
            output.Close();
        }
        catch (Exception exc)
        {
            throw exc;
        }
    }

    public static List<Food> load(string filename)
    {
        List<Food> FoodList = new List<Food>();
        StreamReader input;

        try
        {
            input = File.OpenText("Menu.txt");
        }
        catch (Exception exc)
        {
            throw exc;
        }

        while (!input.EndOfStream)
        {
            string tmp = input.ReadLine();
            string name;
            int cost;


            try
            {
                name = tmp.Split('%')[0];
                cost = int.Parse(tmp.Split('%')[1]);

                if (cost<=0) throw new ArgumentException();

                Food food = new Food(name, cost);

                FoodList.Add(food);
            }
            catch (Exception exc)
            {
                throw exc;
            }
        }
        input.Close();

        FoodList.Sort();

        return FoodList;
    }
}
